var util = require('../../utils/util.js');
var ajax = require('../../utils/ajax.js');
var app = getApp();

Page({
  data: {
    account: '',
    password: '',
  },

  onLoad: function () {
    this._getOpenId();
  },



  /**
   * 账号输入
   */
  _accuntInput: function (e) {
    this.setData({
      account: e.detail.value
    })
    console.log(e.detail.value)
  },

  /**
   * 密码输入
   */
  _pwdInput: function (e) {
    this.setData({
      password: e.detail.value
    })
    console.log(e.detail.value)
  },

  /**
   * 登陆
   */
  _login: function () {

  },

  _getOpenId: function () {
    var $this = this;

    wx.login({
      success: function (res) {
        ajax.G(
          '/openapi/CHome/getUserOpenID',
          {
            jscode: res.code
          },
          function (d) {
            app.setOpenId(d.result.openid);
            $this._openIdLogin(d.result.openid);
          }
        );
      },
      fail: function () { },
      complete: function () { }
    })
  },

  /**
   * openid静默登陆
   */
  _openIdLogin: function (openid) {
    var $this = this;
    ajax.P(
      '/openapi/CHome/login',
      {
        openId: openid
      },
      function (d) {
        app.setUserSid(d.result.sid);
      }
    );
  },
});